<template>
  <div class="chart-container" :style="{ height: containerHeight + 'px' }">
    <div ref="chart" class="chart"></div>
    <!-- 修改按钮文字颜色 -->
    <el-button type="primary" @click="copyChartData" style="color: white; display: block; margin: auto;">
      复制图表数据
    </el-button>
  </div>
</template>

<script>
import { Button } from 'element-ui'
import * as echarts from 'echarts'
import { getAnalysis } from '@/api/largeScreen'
export default {
  components: {
    'el-button': Button
  },
  data() {
    return {
      chartData: [],
      chart: null,
      containerHeight: 500 // 初始高度
    }
  },
  mounted() {
    this.getAnalysis()
    this.chart = echarts.init(this.$refs.chart)
    window.addEventListener('resize', this.resizeChart)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart) // 移除监听
  },
  methods: {
    async getAnalysis() {
      const res = await getAnalysis()
      console.log('left', res)
      this.chartData = res
      console.log('chartData', this.chartData)
      await this.updateChart()
    },
    updateChart() {
      const xAxisData = this.chartData.map(item => item.hostname)
      const cpuData = this.chartData.map(item => item.cpuPer)
      const memData = this.chartData.map(item => item.memPer)
      const option = {

        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
    data: ['CPU使用率 (%)', '内存使用率 (%)'],
    top: 'top',
    textStyle: { // 修改图例文字颜色
      color: '#FFFFFF'
    }
  },
        xAxis: {
          type: 'category',
          data: xAxisData,
          axisLabel: {
            interval: 0,
            rotate: 45,
            color: '#FFFFFF'
          },
          nameTextStyle: { // x轴名称文字样式
    color: '#FFFFFF' // x轴名称文字颜色设置为白色
  }
        },
        yAxis: [
          {
            type: 'value',
            name: 'cpu (%)',
            min: 0,
            max: 100,
            interval: 20,
            axisLabel: {
              formatter: '{value} %',
              color: '#FFFFFF'
            },
    nameTextStyle: { // y轴名称文字样式
      color: '#FFFFFF' // y轴名称文字颜色设置为白色
    }
          },
          {
            type: 'value',
            name: '内存 (%)',
            position: 'right',
            axisLabel: {
              formatter: '{value} %',
              color: '#FFFFFF'
            },
    nameTextStyle: { // y轴名称文字样式
      color: '#FFFFFF' // y轴名称文字颜色设置为白色
    }
          }
        ],
        series: [
          {
            name: 'CPU使用率 (%)',
            type: 'bar',
            data: cpuData,
            itemStyle: { normal: { color: '#ddb926' }
            },
            label: {
              show: true,
              position: 'top',
              color: '#FFFFFF'
            }
          },
          {
            name: '内存使用率 (%)',
            type: 'bar',
            data: memData,
            yAxisIndex: 1, // 使用第二个y轴
            itemStyle: { normal: { color: '#7a8ce9' }
            },
            label: {
              show: true,
              position: 'top',
              color: '#FFFFFF'
            }
          }
        ],
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        }
      }

      this.chart.setOption(option)
    },
    resizeChart() {
      this.containerHeight = this.$refs.chart.offsetHeight // 重新获取容器高度
      this.chart.resize() // 重新渲染图表以适应容器大小
    },
    copyChartData() {
      const dataString = JSON.stringify(this.chartData, null, 2)
      const el = document.createElement('textarea')
      el.value = dataString
      document.body.appendChild(el)
      el.select()
      document.execCommand('copy')
      document.body.removeChild(el)
      alert('数据已复制到剪贴板')
    }
  }
}
</script>

<style scoped>
.chart-container {
  width: 100%;
  /* 确保图表宽度填满其容器 */
  height: 50%;
  /* 自动调整高度以适应宽度 */
  position: relative;
  /* 为.chart添加绝对定位做准备 */
  overflow: hidden;
  /* 防止图表溢出 */
}

.chart {
  width: 100%;
  /* 确保图表宽度填满其容器 */
  height: 50%;
  /* 自动调整高度以适应宽度 */
}
</style>
